<?php

namespace app\admin\controller\qingdongams\finance;

use addons\qingdongams\model\Consume;
use addons\qingdongams\model\Consume as ConsumeModel;
use addons\qingdongams\model\ConsumeDetail;
use addons\qingdongams\model\ExamineRecord;
use app\admin\controller\qingdongams\Base;


/**
 * 出纳管理
 * @icon fa fa-user
 */
class Cashier extends Base {
	protected $relationSearch = true;
	/**
	 * @var \addons\qingdongams\model\Consume
	 */
	protected $model = null;


	public function _initialize() {
		parent::_initialize();
		$this->model = new Consume();
	}


	/**
	 * 查看
	 */
	public function index() {
		//设置过滤方法
		$this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model->where($where)->where(['check_status' => 2])->with(['staff','customer'])
                ->order('id desc')->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }

		return $this->view->fetch();
	}

    //费用详情
    public function detail($ids = null) {
        // 基本信息
        $row = $this->model->where('id',$ids)->with(['staff','customer'])->find();
        if(empty($row)){
            $this->error('信息不存在');
        }
        $row['detail']= ConsumeDetail::where(['consume_id'=>$ids])->select();
        // 操作记录
        $operations = ExamineRecord::where('relation_id',$ids)->with(['checkStaff'])->select();
        $this->assign('ids',$ids);
        $this->assign('row',$row);
        $this->assign('operations',$operations);
        return $this->view->fetch();
    }

    // 回款： 新增订单的回款/列表/详情
    // 费用：列表/新增/修改/删除
    // 出纳：列表/详情/批量打款 单独打款操作 is_cashier=1
    //出纳
    public function cashier(){
        $ids=input('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $ids = rtrim($ids,']');
        $ids = ltrim($ids,'[');
        $ids=explode(',',$ids);
        ConsumeModel::where(['id'=>['in',$ids]])->update(['is_cashier'=>1]);
        $data=[];
        foreach ($ids as $id){
            $data[] = [
                'relation_type' => ExamineRecord::CONSUME_TYPE,
                'relation_id' => $id,
                'check_staff_id' => $this->_staff->id,
                'status' => 1,
                'content' => '出纳审核',
                'createtime' => time(),
                'updatetime' => time()
            ];
        }
        $examineModel=new ExamineRecord();
        $examineModel->insertAll($data);

        $this->success('操作成功');
    }
}
